package com.endurance.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class ProductAdvice {

    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("前置通知-形参：" + Arrays.toString(joinPoint.getArgs()));
        System.out.println("前置通知-方法信息：" + joinPoint.getSignature());
    }

    public void afaterAdvice(JoinPoint joinPoint, Object retVal) {
        System.out.println("后置通知-返回值：" + retVal);
    }

    public void afterThrowingAdvice(JoinPoint joinPoint, Throwable throwable) {
        System.out.println("异常通知-异常信息：" + throwable.getMessage());
    }


    // 后置通知与环绕通知同时存在时，环绕通知的返回值会影响后置通知的返回值
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
        Object retValue;
        try {
            retValue = proceedingJoinPoint.proceed();
            System.out.println("环绕通知-返回值:" + retValue);
        } catch (Throwable e) {
            System.out.println("环绕通知-异常信息：" + e.getMessage());
            throw new RuntimeException(e);
        } finally {
        }
        return retValue;
    }

}
